<script setup>
import {get} from "../../js/http.js";

const emit = defineEmits(['close','save'])
const javaTypes = ref(["String", "int", "boolean", "Date", "Time", "byte","byte[]", "short", "long", "float", "double", "char", "Timestamp", "BigDecimal"])
import {onMounted, ref, reactive} from 'vue';

const isInit = ref(false)
const props = defineProps({
  currentIndex: {
    type: Object,
    required: true
  }
});
onMounted(async()=>{
  if (props.currentIndex.table){
    await getSchemaField(props.currentIndex.conn, props.currentIndex.table)
  }
})
async function getSchemaField(conn, table) {
  let url = "/admin/setting/getSchema";
  let res = await get(url, {conn: conn, table: table})
  res.result.data.forEach(column => {//fieldName  columnName
    const existingIndex = props.currentIndex.fieldsConfig.findIndex(field => field.fieldName === column.columnName);
    if (existingIndex === -1) {
      // 如果不存在，则添加
      props.currentIndex.fieldsConfig.push({fieldName:column.columnName,typeName:column.typeName,meta:column,stored:true});
    } else {
      // 如果存在，则替换
      props.currentIndex.fieldsConfig[existingIndex].meta = column;
    }
  });
  isInit.value=true
}
</script>

<template>
  <div class="modal is-active is-small">
    <div class="modal-background"></div>
    <div style="width:1100px;" class="modal-card">
      <header class="modal-card-head is-small">
        <p class="modal-card-title">索引设置</p>
        <button aria-label="close" class="delete" @click="emit('close')"></button>
      </header>
      <section class="modal-card-body">
        <!--字段设置-->
          <table class="table is-fullwidth">
            <thead>
            <tr>
              <th>排除</th>
              <th>序号</th>
              <th>字段</th>
              <th>原类型</th>
              <th>类型</th>
              <th>存储</th>
              <th>排序</th>
              <th>分组</th>
              <th>分词</th>
              <th>字典</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,itemIndex)  in currentIndex.fieldsConfig" v-if="isInit==true">
              <td> <input type="checkbox" v-model="item.exclude"  />  </td>
              <td> {{item.meta.columnIndex}}  </td>
              <td> {{item.fieldName}}  </td>
              <td> {{item.meta.originalType}}({{item.meta.columnSize}})  </td>
              <td>
                <div class="select is-small">
                  <select  v-model="item.typeName">
                    <option v-for="item in javaTypes" :value="item">{{ item }}</option>
                  </select>
                </div>
              </td>
              <td> <input type="checkbox" v-model="item.stored"  />  </td>
              <th> <input type="checkbox" v-model="item.sort"  />  </th>
              <th> <input type="checkbox" v-model="item.grouping"  />  </th>
              <td> <input type="checkbox" v-model="item.analyzerd"  />  </td>
              <td>
                <input type="checkbox" v-model="item.sets"  />
                <input class="input is-small ml-1" v-if="item.sets==true"  type="text" style="width: 30px" v-model="item.setsSplit" placeholder="分隔符">

              </td>
            </tr>
            </tbody>
          </table>
        <!--字段设置 End-->
      </section>
      <footer class="modal-card-foot is-flex is-justify-content-flex-center">
        <div class="buttons">
          <!--button class="button" @click="fieldAddHandle">增加字段配置</button-->
          <button class="button is-link" @click="emit('save')">保存</button>
        </div>
      </footer>
    </div>
  </div>
</template>

<style scoped>
td{
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
</style>
